﻿var ajaxRequest = null;
var ajaxQueue = new Array();

Library.Ajax = {
    Create: function () {
        Library.Ajax.Progress(true);
        if (ajaxRequest) {
            ajaxRequest.abort();
            ajaxRequest = null;
        }

        if (window.XMLHttpRequest) {
            ajaxRequest = new XMLHttpRequest;
        } else {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
    },

    AddQueue: function (queueElement) {
        ajaxQueue = Library.Array.Add(ajaxQueue, queueElement);
    },

    RunQueue: function () {
        for (var i = 0; i < ajaxQueue.length; i++) {
            if (ajaxQueue[i] != null) {
                eval(ajaxQueue[i]);
                ajaxQueue[i] = null;
            }
        }
    },

    ReplaceParameters: function (url) {
        var urlParams = url.split('{');
        for (var i = 0; i < urlParams.length; i++) {
            if (urlParams[i].indexOf('}') > -1) {
                var urlParam = urlParams[i].split('}');
                url = Library.Util.ReplaceAll(url, '{' + urlParam[0] + '}', Library.UserInterface.GetDetailPageElement(urlParam[0]).value);
            }
        }
        if (url.indexOf('?') > -1)
            url += '&';
        else
            url += '?';

        var dateObject = new Date();
        var uniqueId = dateObject.getFullYear() + '' + dateObject.getMonth() + '' + dateObject.getDate() + '' + dateObject.getTime();
        return url + uniqueId + "&AjaxModule=Library.Ajax";
    },

    Status: function () {
        return (ajaxRequest && ajaxRequest.readyState == 4 && ajaxRequest.status == 200);
    },

    ResponseParser: function () {
        var xmlResponse;
        try {
            xmlResponse = new ActiveXObject("Microsoft.XMLDOM");
            xmlResponse.async = "false";
            xmlResponse.loadXML(ajaxRequest.responseText);
        } catch (e) {
            try {
                parser = new DOMParser();
                xmlResponse = parser.parseFromString(ajaxRequest.responseText, "text/xml");
            } catch (err) {
                window.status = 'Ajax - Hata';
            }
        }

        if (xmlResponse != null && xmlResponse.getElementsByTagName("name").length > 0 && xmlResponse.getElementsByTagName("name")[0].firstChild.data == "Library.Ajax.Exception") {
            Library.Ajax.Exception(xmlResponse.getElementsByTagName("value")[0].firstChild.data);
            return null;
        }
        if (xmlResponse != null && xmlResponse.getElementsByTagName("name").length > 0 && xmlResponse.getElementsByTagName("name")[0].firstChild.data == "Library.Ajax.InfoMessage") {
            Library.Ajax.InfoMessage(xmlResponse.getElementsByTagName("value")[0].firstChild.data);
        }
        return xmlResponse;
    },

    Exception: function (errorMessage) {
        alert(errorMessage);
    },

    InfoMessage: function (infoMessage) {
        document.getElementById("AjaxInfo").innerText = infoMessage;
        setTimeout(function () {
            document.getElementById("AjaxInfo").innerText = "";
        }, 3000);
    },

    Progress: function (start) {
        if (start) {
            document.getElementById("AjaxLoader").style.display = "";
        } else {
            setTimeout(function () {
                document.getElementById("AjaxLoader").style.display = "none";
            }, 100);
        }

        setTimeout(function () {
            //alert('a');
        }, 100);
    }
};